
//#include <stdio.h>
#include <iostream>
#include "l_mobile_service.hpp"
#include "mobile_service.hpp"


namespace http{
namespace server3 {
const char* l_mbl_service_metatable = "l_mbl_service_metatable";
//========================================================================
// mobile_service mobile_serice:new(char* host, int port)                                                                    */
//========================================================================
int l_mbl_service_constructor(lua_State* L)
{
    std::cout << "\nLua has called constructor function";
    if(lua_gettop(L) >= 2)
    {
        //Tao userdata
        if(lua_isstring(L, 2) && lua_isnumber(L, 3)){
            std::string host = lua_tostring(L, 2);            
            int port = lua_tointeger(L, 3);
            mobile_service ** mbl_service_ = (mobile_service**)lua_newuserdata(L, sizeof(mobile_service*));
            *mbl_service_ = new mobile_service(host,port);
            (*mbl_service_)->init();
            (*mbl_service_)->set_cache(server::mobile_cache_ptr_);
            //Get metatable
            luaL_getmetatable(L,l_mbl_service_metatable);
            //gan metatable cho userdata
            lua_setmetatable(L,-2);
            return 1;
        }
    }
    return 0;
}
int l_mbl_service_destructor(lua_State* L)
{
    std::cout << "Call mb destructor.\n";
    mobile_service * mobile_ = l_mbl_service_check(L, 1);
    if(mobile_ != NULL){
        delete mobile_;
        lua_pushboolean(L, 1);
        return 1;
    }
    lua_pushnil(L);
    return 0;
}

mobile_service* l_mbl_service_check(lua_State* L, int n)
{
    if(lua_isuserdata(L, n)){
        //Kiem tra xem userdata co dung la mobile_service ko
        lua_getmetatable(L, n);
        luaL_newmetatable(L, l_mbl_service_metatable);
        int compare = lua_equal(L,-2, -1);
        lua_pop(L, 2);

        if(compare)
            return *(mobile_service**)luaL_checkudata(L, n, l_mbl_service_metatable);                    
    }
    std::cout<<"\n[ERROR] Checked out data is not a mobile service";
    return NULL;
}

int l_mbl_service_parse_user_agent(lua_State* L)
{
    mobile_service* mobile_service_ = l_mbl_service_check(L, 1);
    if(mobile_service_ != NULL){
        if(lua_isstring(L, 2)){
            std::string user_agent_str_ = lua_tostring(L, 2);
            std::string result_;
            if(mobile_service_->parse_user_agent(user_agent_str_,result_)){
                lua_pushlstring(L,result_.c_str(),result_.length());
                return 1;
            }
        }
    }
    return 0;
}

//Called from lua_interface to register function.
int l_register_mbl_service(lua_State* L)
{
    luaL_reg regArr[] = {
        {"new",l_mbl_service_constructor},
        {"parse_user_agent", l_mbl_service_parse_user_agent},
        {"__gc",l_mbl_service_destructor},
        {NULL, NULL}
    };    
    luaL_newmetatable(L,l_mbl_service_metatable);
    luaL_register(L, NULL, regArr);    
    lua_pushvalue(L, -1);
    lua_setfield(L, -1, "__index");
    lua_setglobal(L, "mobile_service");
    return 1;
}
}
}



